<?php

class LightpressPreformatter {
    
    // does not work with the WP post from a pop3 mailbox facility
    
    var $_filters = array(
        'content_save_pre', 'publish_post', 'edit_post');
    
    var $_use_lp_formatting = false;
    
    var $_post_content;
    
    function LightpressPreformatter($preformatter_method='wp') {
        
        if ($preformatter_method == 'lp') {
            $this->_use_lp_formatting = true;
            require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Frontend.php');
        }
        
    }
    
    function content_save_pre($post_content) {
        $this->_post_content = $post_content;
        return $post_content;
    }
    
    function preformat($post_id, &$content) {
        global $wpdb;
        $content = stripslashes($content);
        if ($this->_use_lp_formatting)
            $preformatted =& Frontend::paragrapher($content);
        else
            $preformatted = apply_filters('the_content', $content);
        $preformatted .= chr(0);
        $query = "UPDATE $wpdb->posts SET post_content_filtered = '" . addslashes($preformatted) . "' WHERE ID = '$post_id'";
        $wpdb->query($query);
    }
    
    function publish_post($post_id) {
        if (!is_null($this->_post_content))
            $this->preformat($post_id, $this->_post_content);
        return $post_id;
    }
    
    function edit_post($post_id) {
        if (!is_null($this->_post_content))
            $this->preformat($post_id, $this->_post_content);
        return $post_id;
    }
    
    function register_filters() {
        foreach ($this->_filters as $filter) {
            add_action($filter, array(&$this, $filter), 99);
        }
    }
    
    
}

?>